<?php
namespace app\index\controller;
use PHPMailer\Mail;
use app\common\controller\Common;
use app\index\model\User as UserModel;
use think\Controller;
use think\Loader;
use xbs\Run;

class User extends Common
{
    public function index()
    {
        // if(!session('uid')){
        //     $this->error("你还没有登录，请先登录！");
        // }
        $this->assign('title',"用户中心");
        return $this->fetch();
    }

    public function test()
    {

        $email  = input('post.email');

        $html = <<<xbs
        <div><br></div><div><includetail><div><tincludetail><div style="font:Verdana normal 14px;color:#000;"><div><div><table style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-family: 'Helvetica Neue Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;" cellspacing="0" cellpadding="0" border="0"><tbody style="margin: 0; padding: 0;"><tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"><td style="margin: 0; padding: 0;"><div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><span style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;">2018年7月份</span></div></td></tr><tr style="margin: 0; padding: 0;"><td style="margin: 0; padding: 30px;"><p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;">Hi，你好<em style="font-weight: 700;"></em></p>
        <div style="font-size:14px">
        作为全球范围内应用广泛的开发语言,PHP入选全球五大最受欢迎的编程语言,是Web开发首选语言。<br><br>而广州学并思PHP第15期也在7月15开班<span style="color:red">（小班制,8k）</span>，由刚从企业出来的老师带班，讲的全是企业中实际用到的，课程包含：前端+PHP+MYSQL+linux+微信开发+框架设计+接口设计，详情：<span style="color:red">113664000</span>。</div><p></p><a href="http://www.xuebingsi.com/" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">详情了解</a></td></tr><tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"><td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td></tr></tbody></table></div></div>
</div></tincludetail></div></includetail></div>
xbs;

       $res = Mail::send('6955044@qq.com','活动通知',$html);

       var_dump($res);


       // if($res===true){
       //      $this->success("");
       // }else{
       //      $this->error($res);
       // }
       // $obj = new Run();

       // $obj->show();
        // 113664000@qq.com
        // guytaqjlvyxnbija
        // smtp.qq.comlskdjf
        // ssl
        // 465
    }

    public function set()
    {
        $user = UserModel::get(session('uid'));

        $this->assign('user',$user);
        $this->assign('title',"用户设置");
        return $this->fetch();
    }

    public function save()
    {
        $data = input('post.');

        $data['uid'] = session('uid');

        $validate = Loader::validate('Reg');

        if(!$validate->scene('save')->check($data)){
            $this->error($validate->getError());
        }

        // Db::table('fly_user')->where('uid', session('uid'))->update($data)
        
        $res = (new UserModel)->save($data,['uid'=>session('uid')]);

        if($res===false){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    public function upload()
    {

        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){

                $face = "uploads/".date("Ymd")."/".$info->getFilename();

                $res = (new UserModel)->save(['face'=>$face],['uid'=>session('uid')]);

                unlink(session('face'));

                session('face',$face);

                exit(json_encode(['code'=>0,'msg'=>'上传成功']));

                // $this->error('上传成功');
            }else{

                $this->success($file->getError());
            }
        }
    }
}
